import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'
import { codeInspectorPlugin } from 'code-inspector-plugin'

// https://vitejs.dev/config/
export default defineConfig({
  define: {
    __VUE_PROD_DEVTOOLS__: 'false',
    __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
  },
  
  base: "./",

  // 模块解析配置
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@components': path.resolve(__dirname, './src/components'),
      '@assets': path.resolve(__dirname, './src/assets'),
      '@store': path.resolve(__dirname, './src/store'),
      '@types': path.resolve(__dirname, './src/types'),
      '@utils': path.resolve(__dirname, './src/utils')
    }
  },

  // 代理跨域
  server: {
    open: false, // 启动后是否默认浏览器打开
    port: 3000, // 修改端口避免权限问题
    host: '127.0.0.1', // 使用127.0.0.1而不是::1
    proxy: {
      // 代理所有 /api 开头的请求到后端
      '/api': {
        target: 'http://localhost:8888',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('🔄 代理请求:', req.method, req.url, '→', options.target + req.url);
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('📥 代理响应:', req.url, '状态:', proxyRes.statusCode);
          });
          proxy.on('error', (err, req, res) => {
            console.error('❌ 代理错误:', req.url, err.message);
          });
        }
      },
      // 保持原有的 /dragon 代理
      '/dragon': {
        target: 'http://localhost:8888/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dragon/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('🔄 Dragon代理请求:', req.method, req.url, '→', options.target + req.url);
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('📥 Dragon代理响应:', req.url, '状态:', proxyRes.statusCode);
          });
          proxy.on('error', (err, req, res) => {
            console.error('❌ Dragon代理错误:', req.url, err.message);
          });
        }
      }
    }
  },

  // 构建配置
  build: {
    // 确保生产环境正确构建
    outDir: 'dist',
    assetsDir: 'assets',
    sourcemap: false,
    minify: 'terser', // 明确指定使用terser
    // 构建时移除console
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    rollupOptions: {
      output: {
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: '[ext]/[name]-[hash].[ext]'
      }
    }
  },

  // 插件配置
  plugins: [
    vue(), 
    
    // Code Inspector 插件 - 仅在开发模式下启用
    codeInspectorPlugin({
      bundler: 'vite',
      editor: 'cursor',
      hotKeys: ['altKey', 'shiftKey'],
      showSwitch: true
    }),
    
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        'pinia',
        {
          'naive-ui': [
            'useDialog',
            'useMessage',
            'useNotification',
            'useLoadingBar',
            'useThemeVars',
            'darkTheme',
            'lightTheme',
            'globalThemeOverrides'
          ]
        },
        {
          '@vueuse/core': [
            'useFullscreen',
            'useLocalStorage',
            'usePreferredDark'
          ]
        }
      ],
      dts: true,
      eslintrc: {
        enabled: true
      }
    }),

    Components({
      resolvers: [
        NaiveUiResolver()
      ],
      dts: true
    })
  ],

  // 优化配置
  optimizeDeps: {
    include: [
      'vue',
      'vue-router',
      'pinia',
      'naive-ui',
      '@vueuse/core',
      '@vicons/ionicons5'
    ]
  }
}) 